package com.ipzoe.web.task;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ipzoe.act.domain.Activity;
import com.ipzoe.act.service.IActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
public class ActivityTask {

    @Autowired
    private IActivityService iActivityService;

    /**
     * 活动开始和结束
     */
    public void activityBeginAndExpire(){

        //处理未开始的并且开始时间小于等于今天的 ，开始活动
        LambdaUpdateWrapper<Activity> activityLuw = new LambdaUpdateWrapper<>();
        activityLuw.set(Activity::getStatus, 1);
        activityLuw.eq(Activity::getStatus, 0);
        activityLuw.apply("date_format(begin_time,'%y%m%d') <= date_format({0},'%y%m%d')", DateUtil.today());
        iActivityService.update(activityLuw);

        //处理未过期，结束时间小于等于今天的，结束活动
        LambdaUpdateWrapper<Activity> activityLuw1 = new LambdaUpdateWrapper<>();
        activityLuw1.set(Activity::getStatus, 2);
        activityLuw1.ne(Activity::getStatus, 2);
        activityLuw1.apply("date_format(end_time,'%y%m%d') <= date_format({0},'%y%m%d') ", DateUtil.today());
        iActivityService.update(activityLuw1);
    }
}
